package system

import (
	"github.com/gin-gonic/gin"
	"server/global"
	comRequest "server/model/common/request"
	"server/model/common/response"
	sysModel "server/model/system"
	sysRequest "server/model/system/request"
	sysService "server/service/system"
)

type MenuGroupApi struct {
}

//分组列表
func (g *MenuGroupApi) Index(c *gin.Context) {
	p := &sysRequest.SysMenuGroupList{}
	if err := c.ShouldBind(p); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := sysService.MenuServiceApp.GetMenuGroupList(p)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(response.PageResult{
		List:  list,
		Total: total,
	}, c)
}
func (g *MenuGroupApi) Save(c *gin.Context) {
	p := &sysModel.SysMenuGroup{}
	if err := c.ShouldBind(p); err != nil {
		err = global.GvaValid.TranslateErr(err)
		response.FailWithMessage(err.Error(), c)
		return
	}
	//设置表单数据，方便保存日志
	rows, err := sysService.MenuServiceApp.SaveMenuGroup(p)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(gin.H{
		"rows": rows,
	}, c)
}

//删除
func (g *MenuGroupApi) Destroy(c *gin.Context) {
	p := &comRequest.IdsReq{}
	if err := c.ShouldBind(p); err != nil {
		err = global.GvaValid.TranslateErr(err)
		response.FailWithMessage(err.Error(), c)
		return
	}
	rows, err := sysService.MenuServiceApp.DelMenuGroup(p)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(gin.H{
		"rows": rows,
	}, c)
}
